﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using CMN.Controllers.Filters;
using System.Text;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Sys_UserRolesController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回用户角色表列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组

            int userId = Utils.StringToInt(actionId,0);
            QueryParam qp = new QueryParam();
            qp.PageIndex =1;
            qp.PageSize = Int32.MaxValue;
            int recordNum = 0;
            IDataService<Sys_RolesEntity> Sys_RolesSv = ServiceBuilder.BuildSys_RolesService();
            List<Sys_RolesEntity> Sys_RolesList = Sys_RolesSv.List(qp, out recordNum);
            StringBuilder sb = new StringBuilder();
            sb.Append("{'action':true,'message':'error!','data':[");
            for(int i=0;i<Sys_RolesList.Count;i++)
            {
                if (i == (Sys_RolesList.Count-1))
                {
                    sb.Append(string.Concat("[", Sys_RolesList[i].RoleID, ",'", Sys_RolesList[i].R_RoleName, "']"));
                }
                else
                {
                    sb.Append(string.Concat("[", Sys_RolesList[i].RoleID, ",'", Sys_RolesList[i].R_RoleName, "'],"));
                }
            }
            sb.Append("],values:[");
            qp.Orderfld = null;
            qp.Where = string.Concat("R_userid=", userId.ToString());
            IDataService<Sys_UserRolesEntity> Sys_UserRolesSv = ServiceBuilder.BuildSys_UserRolesService();
            List<Sys_UserRolesEntity> sys_userrolesList = Sys_UserRolesSv.List(qp, out recordNum);
            for (int i = 0; i < sys_userrolesList.Count; i++)
            {
                if (i == (sys_userrolesList.Count-1))
                {
                    sb.Append(sys_userrolesList[i].R_RoleID.ToString());
                }
                else
                {
                    sb.Append(string.Concat(sys_userrolesList[i].R_RoleID.ToString(), ","));
                }
            }
            sb.Append("]}");

            return sb.ToString(); 
        }

        /// <summary>
        /// 查看用户角色表 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Sys_UserRolesEntity> Sys_UserRolesSv = ServiceBuilder.BuildSys_UserRolesService();
            int intId=Utils.StringToInt(actionId,0);
            Sys_UserRolesEntity sys_userroles = Sys_UserRolesSv.Look(intId);
            if (sys_userroles != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(sys_userroles), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新用户角色表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int userId = Utils.StringToInt(actionId,0);
            if(userId==0){
               return  returnJson = "{success:'false'}";
            }
            string roles = (string)Utils.GetQueryAndFormString("userroles", this, 1000, 1, DataType.Str);
            roles = Utils.UrlDecode(roles);
            if (string.IsNullOrEmpty(roles))
            {
                return returnJson = "{success:'false'}";
            }
            IDataService<Sys_UserRolesEntity> Sys_UserRolesSv = ServiceBuilder.BuildSys_UserRolesService();
            Sys_UserRolesSv.DeleteList(userId.ToString());
            List<string> roleList = Utils.StringToArray(roles, ',', false);
            Sys_UserRolesEntity userRole = new Sys_UserRolesEntity();
            
            foreach (string role in roleList)
            {
                userRole.R_RoleID=Utils.StringToInt(role,0);
                userRole.R_UserID=userId;
                Sys_UserRolesSv.Insert(userRole);
            }
            
           
                returnJson = "{success:'true'}";
           
             


            return returnJson;
        }

        /// <summary>
        /// 新增用户角色表
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Sys_UserRolesEntity sys_userroles = new Sys_UserRolesEntity();
            sys_userroles.DataTable_Action = DataTableAction.Insert;
            IDataService<Sys_UserRolesEntity> Sys_UserRolesSv = ServiceBuilder.BuildSys_UserRolesService();
            if (Sys_UserRolesSv.Insert(sys_userroles) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除用户角色表
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Sys_UserRolesEntity sys_userroles = new Sys_UserRolesEntity();
                sys_userroles.R_UserID = intId;
                sys_userroles.R_RoleID = intId;
                IDataService<Sys_UserRolesEntity> Sys_UserRolesSv = ServiceBuilder.BuildSys_UserRolesService();
                if (Sys_UserRolesSv.Delete(sys_userroles) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除用户角色表
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Sys_UserRolesEntity> Sys_UserRolesSv = ServiceBuilder.BuildSys_UserRolesService();
                if (Sys_UserRolesSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
